package com.by.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.afollestad.materialdialogs.MaterialDialog;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.entity.Password;
import com.by.yz.loginUtil.PasswordRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 123 on 2016/10/7.
 */
public class ResetPasswordActivity extends BaseActivity {
    private  EditText new_password;
    private String phone;
    private Password password;
    private Button sure;
    private String newpwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reset_password_activity);
        setToolbarTitle("修改密码");
        initView();
        initDatas();
    }

    private void initDatas() {

    }

    private void initView() {
        final Intent intent = getIntent();
        phone = intent.getStringExtra("number").toString();
        Log.e("myPhone2 = ",phone);
        new_password=(EditText)findViewById(R.id.new_password);
        sure=(Button)findViewById(R.id.sure);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newpwd=new_password.getText().toString();
                password=new Password();
                password.setLogin(phone);
                password.setNewPassword(newpwd);
                JSONObject data = ToJSonObject.beanToJSONObejct(password);
                if(newpwd.length()<6){
                    new MaterialDialog.Builder(ResetPasswordActivity.this)
                            .content("请输入不少于6位的密码")
                            .show();
                }else{
                    PasswordRESTFul passwordRESTFul=new PasswordRESTFul();
                    passwordRESTFul.postPassword(phone,new_password.getText().toString(), data, new PasswordRESTFul.PostPwd() {
                        @Override
                        public void ok(JSONObject response) throws JSONException {
                            final MaterialDialog registerSuccess = new MaterialDialog.Builder(ResetPasswordActivity.this)
                                    .content("修改密码成功,请重新登录")
                                    .show();
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                public void run() {
                                    //execute the task
                                    registerSuccess.dismiss();
                                    intent.setClass(ResetPasswordActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }, 2000);
                        }

                        @Override
                        public void error(String error) {
                            Log.e("修改密码失败","eee"+error);
                        }
                    });
                }
            }
        });
    }

}